<template>
  <div class="facilitiesContainer">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch">
      <el-form-item label="设施名称" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入设施名称" clearable style="width: 200px"
          @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="选择类型" clearable style="width: 200px">
          <el-option v-for="num in [1, 2, 3, 4, 5]" :key="num" :label="num" :value="num" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus" @click="handleAdd">新增设施</el-button>
      </el-col>
    </el-row>

    <el-table v-if="refreshTable" v-loading="loading" :data="facilityList" row-key="id">
      <el-table-column prop="name" label="设施名称" width="120"></el-table-column>
      <el-table-column prop="type" label="类型" width="100"></el-table-column>
      <el-table-column prop="location" label="位置" width="300"></el-table-column>
      <el-table-column prop="description" label="描述" width="200"></el-table-column>
      <el-table-column prop="usageInfo" label="使用信息" width="150"></el-table-column>
      <el-table-column prop="usageHours" label="使用时间" width="150"></el-table-column>
      <el-table-column prop="isOccupied" label="占用状态" width="120">
        <template #default="scope">
          <span>{{ scope.row.isOccupied ? '已占用' : '未占用' }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="storeId" label="商店ID" width="150"></el-table-column>
      <el-table-column label="操作" align="center" fixed="right" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog :title="title" v-model="open" width="600px" append-to-body>
      <el-form ref="facilityRef" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="设施名称" prop="name">
              <el-input v-model="form.name" placeholder="请输入设施名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="类型" prop="type">
              <el-select v-model="form.type" placeholder="选择类型">
                <el-option v-for="num in [1, 2, 3, 4, 5]" :key="num" :label="num" :value="num" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="位置" prop="location">
              <el-input v-model="form.location" placeholder="请输入位置（四个顶点）" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="描述" prop="description">
              <el-input v-model="form.description" placeholder="请输入描述" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="使用信息" prop="usageInfo">
              <el-input v-model="form.usageInfo" placeholder="请输入使用信息" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="使用时间" prop="usageHours">
              <el-input v-model="form.usageHours" placeholder="请输入使用时间" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="占用状态" prop="isOccupied">
              <el-select v-model="form.isOccupied" placeholder="选择占用状态">
                <el-option label="未占用" :value="0" />
                <el-option label="已占用" :value="1" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="虚拟建筑" prop="storeId">
              <el-select v-model="form.storeId" placeholder="请选择商店">
                <el-option v-for="store in storesList" :key="store.id" :label="store.name" :value="store.id" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="FacilitiesList">
import { ref, reactive } from 'vue';
import { listFacilities, addFacility, updateFacility, deleteFacility } from "@/api/pages/facility";
//获取虚拟商店的列表
import { listStores } from "@/api/pages/virtualStore";

const loading = ref(true);
const open = ref(false);
const showSearch = ref(true);
const title = ref("");
const refreshTable = ref(true);
const facilityList = ref([]);
const storesList = ref([]);
const form = reactive({
  id: undefined,
  name: "",
  type: 1,
  location: "",
  description: "",
  usageInfo: "",
  usageHours: "",
  isOccupied: 0,
  storeId: undefined,
});
const queryParams = reactive({
  name: "",
  type: undefined,
});
const rules = {
  name: [{ required: true, message: "设施名称不能为空", trigger: "blur" }],
  type: [{ required: true, message: "类型不能为空", trigger: "change" }],
  location: [{ required: true, message: "位置不能为空", trigger: "blur" }],
  description: [{ required: true, message: "描述不能为空", trigger: "blur" }],
  usageInfo: [{ required: true, message: "使用信息不能为空", trigger: "blur" }],
  usageHours: [{ required: true, message: "使用时间不能为空", trigger: "blur" }],
};

// 查询设施列表
const getList = () => {
  loading.value = true;
  listFacilities(queryParams).then(response => {
    facilityList.value = response.data;
    loading.value = false;
  });
};

// 获取虚拟商店列表
const getStores = () => {
  listStores().then(response => {
    storesList.value = response.data;
    console.log("虚拟商店列表", storesList.value);
  });
};

// 取消按钮
const cancel = () => {
  open.value = false;
  reset();
};

// 重置表单
const reset = () => {
  Object.assign(form, {
    id: undefined,
    name: "",
    type: 1,
    location: "",
    description: "",
    usageInfo: "",
    usageHours: "",
    isOccupied: 0,
  });
};

// 搜索按钮操作
const handleQuery = () => {
  getList();
};

// 重置按钮操作
const resetQuery = () => {
  Object.assign(queryParams, {
    name: "",
    type: undefined,
  });
  getList();
};

// 新增按钮操作
const handleAdd = () => {
  reset();
  open.value = true;
  getStores();
  title.value = "新增设施";
};

// 修改按钮操作
const handleUpdate = (row) => {
  Object.assign(form, row);
  open.value = true;
  title.value = "修改设施";
};

// 提交按钮
const submitForm = () => {
  const apiCall = form.id ? updateFacility : addFacility;
  apiCall(form).then(() => {
    getList();
    open.value = false;
  });
};

// 删除按钮操作
const handleDelete = (row) => {
  deleteFacility(row.id).then(() => {
    getList();
  });
};

// 初始化设施列表
getList();
</script>

<style scoped>
.facilitiesContainer {
  padding: 20px;
}

.mb8 {
  margin-bottom: 8px;
}

.dialog-footer {
  text-align: right;
}
</style>